package com.winit.openapi.sms;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.winit.common.query.Page;
import com.winit.common.spi.SPIException;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.BpartnerService;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.sms.common.SmsUtils;
import com.winit.openapi.util.DateUtil;
import com.winit.sms.spi.common.vo.PageVo;
import com.winit.sms.spi.wallet.transactionline.TransactionLineService;
import com.winit.sms.spi.wallet.transactionline.command.TransactionLineCommand;
import com.winit.sms.spi.wallet.transactionline.vo.TransactionLineVo;

/**
 * 钱包扣款明细
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * jun.lu 	1.0  		2016年1月11日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping("/sms/transactionLine")
public class TransactionLineController extends BaseController {

    private static Logger          logger = LoggerFactory.getLogger(TransactionLineController.class);

    @Autowired
    private TransactionLineService transactionLineService;

    @Resource
    private BpartnerService        bpartnerService;

    /**
     * 钱包交易明细查询
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("/pageTransactionLine")
    public String pageTransactionLine(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        TransactionLineCommand command = SmsUtils.toJavaObject(requestMsg.getData(), TransactionLineCommand.class);

        CommandContext ctx = CommandContext.getContext();
        if(null == ctx){
        	logger.info(">>>>>>>>调用pageTransactionLine接口异常，ctx对象为空");
        	return ERROR;
        }
        command.setCtx(ctx);
        /*BpartnerCommand bpartnerCommand = new BpartnerCommand();
        bpartnerCommand.setcBpartnerId(Long.valueOf(ctx.getUserId()));
        Bpartner bpartner = bpartnerService.queryBpartnerById(bpartnerCommand);*/
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        if (bpartner == null) {
            logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>bpartner对象为空");
            return ERROR;
        }

        PageVo pageVo = SmsUtils.SetPageParams(requestMsg.getData());
        command.setPageVo(pageVo);
        command.setCustomerCode(bpartner.getValue());
        if (null != command.getTransactionDateTo()) {
            command.setTransactionDateTo(DateUtil.spliceDate(command.getTransactionDateTo()));
        }
        try {
            Page<TransactionLineVo> pagelist = transactionLineService.pageTransactionLine(command);
            Map<String, Object> data = SmsUtils.setPageData(pagelist);
            responseMsg.setData(data);
        } catch (Exception e) {
            logger.info("<======pageTransactionLine：", e);
        }
        return SUCCESS;
    }

    /**
     * 钱包交易明细导出
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("/exportTransactionLine")
    public String exportTransactionLine(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        TransactionLineCommand command = SmsUtils.toJavaObject(requestMsg.getData(), TransactionLineCommand.class);

        CommandContext ctx = CommandContext.getContext();
        command.setCtx(ctx);
        /*BpartnerCommand bpartnerCommand = new BpartnerCommand();
        bpartnerCommand.setcBpartnerId(Long.valueOf(ctx.getUserId()));
        Bpartner bpartner = bpartnerService.queryBpartnerById(bpartnerCommand);*/
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        if (bpartner == null) {
            logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>bpartner对象为空");
            return ERROR;
        }
        command.setCustomerCode(bpartner.getValue());
        if (null != command.getTransactionDateTo()) {
            command.setTransactionDateTo(DateUtil.spliceDate(command.getTransactionDateTo()));
        }
        String path = null;
        try {
            path = transactionLineService.exportTransactionLine(command);
            logger.info("钱包交易明细导出路径：{}", path);
        } catch (SPIException e) {
            logger.error(e.getMessage());
            responseMsg.setCode(e.getErrorCode());
            responseMsg.setMsg(e.getErrorMsg());
            responseMsg.setData(com.winit.sms.spi.common.constant.Constants.UPLOAD_FA);
            return ERROR;
        }
        responseMsg.setData(path);
        return SUCCESS;
    }

}
